home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / a_utils / _archvrs / mac / unzip201.cpt / UnZip 2.0.1 Docs.rsrc / bin.rsrc_TEXT_129.txt < prev    next >
Text File  |  1993-01-20  |  32KB  |  899 lines

  1. The following is a copy of the file "APPNOTE.TXT" from the file PKZ193A.EXE. This file describes the .ZIP file format and the archiving/dearchiving algorithms used by PKZIP and PKUNZIP. 
  2.  
  3.  
  4. Disclaimer
  5. ----------
  6.  
  7. Although PKWARE will attempt to supply current and accurate
  8. information relating to its file formats, algorithms, and the
  9. subject programs, the possibility of error can not be eliminated.
  10. PKWARE therefore expressly disclaims any warranty that the
  11. information contained in the associated materials relating to the
  12. subject programs and/or the format of the files created or
  13. accessed by the subject programs and/or the algorithms used by
  14. the subject programs, or any other matter, is current, correct or
  15. accurate as delivered.  Any risk of damage due to any possible
  16. inaccurate information is assumed by the user of the information.
  17. Furthermore, the information relating to the subject programs
  18. and/or the file formats created or accessed by the subject
  19. programs and/or the algorithms used by the subject programs is
  20. subject to change without notice.
  21.  
  22.  
  23. General Format of a ZIP file
  24. ----------------------------
  25.  
  26.   Files stored in arbitrary order.  Large zipfiles can span multiple
  27.   diskette media.
  28.  
  29.   Overall zipfile format:
  30.  
  31.     [local file header+file data] . . .
  32.     [central directory] end of central directory record
  33.  
  34.  
  35.   A.  Local file header:
  36.  
  37.     local file header signature          4 bytes  (0x04034b50)
  38.     version needed to extract            2 bytes
  39.     general purpose bit flag             2 bytes
  40.     compression method                      2 bytes
  41.     last mod file time                      2 bytes
  42.     last mod file date                      2 bytes
  43.     crc-32                                     4 bytes
  44.     compressed size                            4 bytes
  45.     uncompressed size                       4 bytes
  46.     filename length                            2 bytes
  47.     extra field length                      2 bytes
  48.  
  49.     filename (variable size)
  50.     extra field (variable size)
  51.  
  52.  
  53.   B.  Central directory structure:
  54.  
  55.       [file header] . . .  end of central dir record
  56.  
  57.       File header:
  58.  
  59.     central file header signature        4 bytes  (0x02014b50)
  60.     version made by                            2 bytes
  61.     version needed to extract            2 bytes
  62.     general purpose bit flag             2 bytes
  63.     compression method                      2 bytes
  64.     last mod file time                      2 bytes
  65.     last mod file date                      2 bytes
  66.     crc-32                                     4 bytes
  67.     compressed size                            4 bytes
  68.     uncompressed size                       4 bytes
  69.     filename length                            2 bytes
  70.     extra field length                      2 bytes
  71.     file comment length                     2 bytes
  72.     disk number start                       2 bytes
  73.     internal file attributes             2 bytes
  74.     external file attributes             4 bytes
  75.     relative offset of local header      4 bytes
  76.  
  77.     filename (variable size)
  78.     extra field (variable size)
  79.     file comment (variable size)
  80.  
  81.       End of central dir record:
  82.  
  83.     end of central dir signature         4 bytes  (0x06054b50)
  84.     number of this disk                     2 bytes
  85.     number of the disk with the
  86.     start of the central directory       2 bytes
  87.     total number of entries in
  88.     the central dir on this disk         2 bytes
  89.     total number of entries in
  90.     the central dir                            2 bytes
  91.     size of the central directory     4 bytes
  92.     offset of start of central
  93.     directory with respect to
  94.     the starting disk number             4 bytes
  95.     zipfile comment length                  2 bytes
  96.     zipfile comment (variable size)
  97.  
  98.  
  99.  
  100.  
  101.   C.  Explanation of fields:
  102.  
  103.       version made by
  104.  
  105.       The upper byte indicates the host system (OS) for the
  106.       file.  Software can use this information to determine
  107.       the line record format for text files etc.  The current
  108.       mappings are:
  109.  
  110.       0 - MS-DOS and OS/2 (F.A.T. file systems)
  111.       1 - Amiga                    2 - VMS
  112.       3 - *nix                     4 - VM/CMS
  113.       5 - Atari ST        6 - OS/2 H.P.F.S.
  114.       7 - Macintosh          8 - Z-System
  115.       9 - CP/M                     10 thru 255 - unused
  116.  
  117.       The lower byte indicates the version number of the
  118.       software used to encode the file.  The value/10
  119.       indicates the major version number, and the value
  120.       mod 10 is the minor version number.
  121.  
  122.       version needed to extract
  123.  
  124.       The minimum software version needed to extract the
  125.       file, mapped as above.
  126.  
  127.       general purpose bit flag:
  128.  
  129.    bit 0: If set, indicates that the file is encrypted.
  130.  
  131.    (For Method 6 - Imploding)
  132.    bit 1: If the compression method used was type 6,
  133.                 Imploding, then this bit, if set, indicates
  134.                 an 8K sliding dictionary was used.  If clear,
  135.                 then a 4K sliding dictionary was used.
  136.    bit 2: If the compression method used was type 6,
  137.                 Imploding, then this bit, if set, indicates
  138.                 an 3 Shannon-Fano trees were used to encode the
  139.                 sliding dictionary output.  If clear, then 2
  140.                 Shannon-Fano trees were used.
  141.  
  142.    (For Method 8)
  143.    bit 1: If this bit is set, the file was compressed with
  144.           the maximum compression version of the type 8
  145.           compression algorithm.  (-ex option)
  146.    bit 2: If this bit is set, the file was compressed with
  147.           the fastest version of the type 8 compression
  148.           algorithm.  (-es option)
  149.  
  150.       Note:  Bits 1 and 2 are undefined if the compression
  151.                  method is any other.
  152.  
  153.    The upper three bits are reserved and used internally
  154.       by the software when processing the zipfile.  The
  155.       remaining bits are unused in version 1.0.
  156.  
  157.       compression method:
  158.  
  159.       (see accompanying documentation for algorithm
  160.       descriptions)
  161.  
  162.       0 - The file is stored (no compression)
  163.       1 - The file is Shrunk
  164.       2 - The file is Reduced with compression factor 1
  165.       3 - The file is Reduced with compression factor 2
  166.       4 - The file is Reduced with compression factor 3
  167.       5 - The file is Reduced with compression factor 4
  168.    6 - The file is Imploded
  169.    7 - Reserved for Tokenizing compression alogrithm
  170.    8 - The algorithm used by the latest version of
  171.        PKZIP/PKUNZIP.
  172.  
  173.       date and time fields:
  174.  
  175.       The date and time are encoded in standard MS-DOS
  176.       format.
  177.  
  178.       CRC-32:
  179.  
  180.       The CRC-32 algorithm was generously contributed by
  181.       David Schwaderer and can be found in his excellent
  182.       book "C Programmers Guide to NetBIOS" published by
  183.       Howard W. Sams & Co. Inc.  The 'magic number' for
  184.       the CRC is 0xdebb20e3.  The proper CRC pre and post
  185.       conditioning is used, meaning that the CRC register
  186.       is pre-conditioned with all ones (a starting value
  187.       of 0xffffffff) and the value is post-conditioned by
  188.       taking the one's complement of the CRC residual.
  189.     
  190.       compressed size:
  191.       uncompressed size:
  192.  
  193.       The size of the file compressed and uncompressed,
  194.       respectively.
  195.  
  196.       filename length:
  197.       extra field length:
  198.       file comment length:
  199.  
  200.       The length of the filename, extra field, and comment
  201.       fields respectively.  The combined length of any
  202.       directory record and these three fields should not
  203.       generally exceed 65,535 bytes.
  204.  
  205.       disk number start:
  206.  
  207.       The number of the disk on which this file begins.
  208.  
  209.       internal file attributes:
  210.  
  211.       The lowest bit of this field indicates, if set, that
  212.       the file is apparently an ASCII or text file.  If not
  213.       set, that the file apparently contains binary data.
  214.       The remaining bits are unused in version 1.0.
  215.  
  216.       external file attributes:
  217.  
  218.       The mapping of the external attributes is
  219.       host-system dependent (see 'version made by').  For
  220.       MS-DOS, the low order byte is the MS-DOS directory
  221.       attribute byte.
  222.  
  223.       relative offset of local header:
  224.  
  225.       This is the offset from the start of the first disk on
  226.       which this file appears, to where the local header should
  227.       be found.
  228.  
  229.       filename:
  230.  
  231.       The name of the file, with optional relative path.
  232.       The path stored should not contain a drive or
  233.       device letter, or a leading slash.  All slashes
  234.       should be forward slashes '/' as opposed to
  235.       backwards slashes '\' for compatibility with Amiga
  236.       and Unix file systems etc.
  237.  
  238.       extra field:
  239.  
  240.       This is for future expansion.  If additional information
  241.       needs to be stored in the future, it should be stored
  242.       here.  Earlier versions of the software can then safely
  243.       skip this file, and find the next file or header.  This
  244.       field will be 0 length in version 1.0.
  245.  
  246.       In order to allow different programs and different types
  247.       of information to be stored in the 'extra' field in .ZIP
  248.       files, the following structure should be used for all
  249.       programs storing data in this field:
  250.  
  251.       header1+data1 + header2+data2 . . .
  252.  
  253.       Each header should consist of:
  254.  
  255.         Header ID - 2 bytes
  256.         Data Size - 2 bytes
  257.  
  258.       Note: all fields stored in Intel low-byte/high-byte order.
  259.  
  260.       The Header ID field indicates the type of data that is in
  261.       the following data block.
  262.  
  263.       Header ID's of 0 thru 31 are reserved for use by PKWARE.
  264.       The remaining ID's can be used by third party vendors for
  265.       proprietary usage.
  266.  
  267.       The Data Size field indicates the size of the following
  268.       data block. Programs can use this value to skip to the
  269.       next header block, passing over any data blocks that are
  270.       not of interest.
  271.  
  272.       Note: As stated above, the size of the entire .ZIP file
  273.          header, including the filename, comment, and extra
  274.          field should not exceed 64K in size.
  275.  
  276.       In case two different programs should appropriate the same
  277.       Header ID value, it is strongly recommended that each
  278.       program place a unique signature of at least two bytes in
  279.       size (and preferably 4 bytes or bigger) at the start of
  280.       each data area.  Every program should verify that it's
  281.       unique signature is present, in addition to the Header ID
  282.       value being correct, before assuming that it is a block of
  283.       known type.
  284.  
  285.       file comment:
  286.  
  287.       The comment for this file.
  288.  
  289.       number of this disk:
  290.  
  291.       The number of this disk, which contains central
  292.       directory end record.
  293.  
  294.       number of the disk with the start of the central directory:
  295.  
  296.       The number of the disk on which the central
  297.       directory starts.
  298.  
  299.       total number of entries in the central dir on this disk:
  300.  
  301.       The number of central directory entries on this disk.
  302.     
  303.       total number of entries in the central dir:
  304.  
  305.       The total number of files in the zipfile.
  306.  
  307.  
  308.       size of the central directory:
  309.  
  310.       The size (in bytes) of the entire central directory.
  311.  
  312.       offset of start of central directory with respect to
  313.       the starting disk number:
  314.  
  315.       Offset of the start of the central direcory on the
  316.       disk on which the central directory starts.
  317.  
  318.       zipfile comment length:
  319.  
  320.       The length of the comment for this zipfile.
  321.  
  322.       zipfile comment:
  323.  
  324.       The comment for this zipfile.
  325.  
  326.  
  327.   D.  General notes:
  328.  
  329.       1)  All fields unless otherwise noted are unsigned and stored
  330.       in Intel low-byte:high-byte, low-word:high-word order.
  331.  
  332.       2)  String fields are not null terminated, since the
  333.       length is given explicitly.
  334.  
  335.       3)  Local headers should not span disk boundries.  Also, even
  336.       though the central directory can span disk boundries, no
  337.       single record in the central directory should be split
  338.       across disks.
  339.  
  340.       4)  The entries in the central directory may not necessarily
  341.       be in the same order that files appear in the zipfile.
  342.  
  343. UnShrinking - Method 1
  344. ----------------------
  345.  
  346. Shrinking is a Dynamic Ziv-Lempel-Welch compression algorithm
  347. with partial clearing.  The initial code size is 9 bits, and
  348. the maximum code size is 13 bits.  Shrinking differs from
  349. conventional Dynamic Ziv-lempel-Welch implementations in several
  350. respects:
  351.  
  352. 1)  The code size is controlled by the compressor, and is not
  353.     automatically increased when codes larger than the current
  354.     code size are created (but not necessarily used).  When
  355.     the decompressor encounters the code sequence 256
  356.     (decimal) followed by 1, it should increase the code size
  357.     read from the input stream to the next bit size.  No
  358.     blocking of the codes is performed, so the next code at
  359.     the increased size should be read from the input stream
  360.     immediately after where the previous code at the smaller
  361.     bit size was read.  Again, the decompressor should not
  362.     increase the code size used until the sequence 256,1 is
  363.     encountered.
  364.  
  365. 2)  When the table becomes full, total clearing is not
  366.     performed.  Rather, when the compresser emits the code
  367.     sequence 256,2 (decimal), the decompressor should clear
  368.     all leaf nodes from the Ziv-Lempel tree, and continue to
  369.     use the current code size.  The nodes that are cleared
  370.     from the Ziv-Lempel tree are then re-used, with the lowest
  371.     code value re-used first, and the highest code value
  372.     re-used last.  The compressor can emit the sequence 256,2
  373.     at any time.
  374.  
  375.  
  376.  
  377. Expanding - Methods 2-5
  378. -----------------------
  379.  
  380. The Reducing algorithm is actually a combination of two
  381. distinct algorithms.  The first algorithm compresses repeated
  382. byte sequences, and the second algorithm takes the compressed
  383. stream from the first algorithm and applies a probabilistic
  384. compression method.
  385.  
  386. The probabilistic compression stores an array of 'follower
  387. sets' S(j), for j=0 to 255, corresponding to each possible
  388. ASCII character.  Each set contains between 0 and 32
  389. characters, to be denoted as S(j)[0],...,S(j)[m], where m<32.
  390. The sets are stored at the beginning of the data area for a
  391. Reduced file, in reverse order, with S(255) first, and S(0)
  392. last.
  393.  
  394. The sets are encoded as { N(j), S(j)[0],...,S(j)[N(j)-1] },
  395. where N(j) is the size of set S(j).  N(j) can be 0, in which
  396. case the follower set for S(j) is empty.  Each N(j) value is
  397. encoded in 6 bits, followed by N(j) eight bit character values
  398. corresponding to S(j)[0] to S(j)[N(j)-1] respectively.  If
  399. N(j) is 0, then no values for S(j) are stored, and the value
  400. for N(j-1) immediately follows.
  401.  
  402. Immediately after the follower sets, is the compressed data
  403. stream.  The compressed data stream can be interpreted for the
  404. probabilistic decompression as follows:
  405.  
  406.  
  407. let Last-Character <- 0.
  408. loop until done
  409.     if the follower set S(Last-Character) is empty then
  410.     read 8 bits from the input stream, and copy this
  411.     value to the output stream.
  412.     otherwise if the follower set S(Last-Character) is non-empty then
  413.     read 1 bit from the input stream.
  414.     if this bit is not zero then
  415.         read 8 bits from the input stream, and copy this
  416.         value to the output stream.
  417.     otherwise if this bit is zero then
  418.         read B(N(Last-Character)) bits from the input
  419.         stream, and assign this value to I.
  420.         Copy the value of S(Last-Character)[I] to the
  421.         output stream.
  422.     
  423.     assign the last value placed on the output stream to
  424.     Last-Character.
  425. end loop
  426.  
  427.  
  428. B(N(j)) is defined as the minimal number of bits required to
  429. encode the value N(j)-1.
  430.  
  431.  
  432. The decompressed stream from above can then be expanded to
  433. re-create the original file as follows:
  434.  
  435.  
  436. let State <- 0.
  437.  
  438. loop until done
  439.     read 8 bits from the input stream into C.
  440.     case State of
  441.     0:  if C is not equal to DLE (144 decimal) then
  442.         copy C to the output stream.
  443.         otherwise if C is equal to DLE then
  444.         let State <- 1.
  445.  
  446.     1:  if C is non-zero then
  447.         let V <- C.
  448.         let Len <- L(V)
  449.         let State <- F(Len).
  450.         otherwise if C is zero then
  451.         copy the value 144 (decimal) to the output stream.
  452.         let State <- 0
  453.  
  454.     2:  let Len <- Len + C
  455.         let State <- 3.
  456.  
  457.     3:  move backwards D(V,C) bytes in the output stream
  458.         (if this position is before the start of the output
  459.         stream, then assume that all the data before the
  460.         start of the output stream is filled with zeros).
  461.         copy Len+3 bytes from this position to the output stream.
  462.         let State <- 0.
  463.     end case
  464. end loop
  465.  
  466.  
  467. The functions F,L, and D are dependent on the 'compression
  468. factor', 1 through 4, and are defined as follows:
  469.  
  470. For compression factor 1:
  471.     L(X) equals the lower 7 bits of X.
  472.     F(X) equals 2 if X equals 127 otherwise F(X) equals 3.
  473.     D(X,Y) equals the (upper 1 bit of X) * 256 + Y + 1.
  474. For compression factor 2:
  475.     L(X) equals the lower 6 bits of X.
  476.     F(X) equals 2 if X equals 63 otherwise F(X) equals 3.
  477.     D(X,Y) equals the (upper 2 bits of X) * 256 + Y + 1.
  478. For compression factor 3:
  479.     L(X) equals the lower 5 bits of X.
  480.     F(X) equals 2 if X equals 31 otherwise F(X) equals 3.
  481.     D(X,Y) equals the (upper 3 bits of X) * 256 + Y + 1.
  482. For compression factor 4:
  483.     L(X) equals the lower 4 bits of X.
  484.     F(X) equals 2 if X equals 15 otherwise F(X) equals 3.
  485.     D(X,Y) equals the (upper 4 bits of X) * 256 + Y + 1.
  486.  
  487.  
  488. Imploding - Method 6
  489. --------------------
  490.  
  491. The Imploding algorithm is actually a combination of two distinct
  492. algorithms.  The first algorithm compresses repeated byte
  493. sequences using a sliding dictionary.  The second algorithm is
  494. used to compress the encoding of the sliding dictionary ouput,
  495. using multiple Shannon-Fano trees.
  496.  
  497. The Imploding algorithm can use a 4K or 8K sliding dictionary
  498. size. The dictionary size used can be determined by bit 1 in the
  499. general purpose flag word, a 0 bit indicates a 4K dictionary
  500. while a 1 bit indicates an 8K dictionary.
  501.  
  502. The Shannon-Fano trees are stored at the start of the compressed
  503. file. The number of trees stored is defined by bit 2 in the
  504. general purpose flag word, a 0 bit indicates two trees stored, a
  505. 1 bit indicates three trees are stored.  If 3 trees are stored,
  506. the first Shannon-Fano tree represents the encoding of the
  507. Literal characters, the second tree represents the encoding of
  508. the Length information, the third represents the encoding of the
  509. Distance information.  When 2 Shannon-Fano trees are stored, the
  510. Length tree is stored first, followed by the Distance tree.
  511.  
  512. The Literal Shannon-Fano tree, if present is used to represent
  513. the entire ASCII character set, and contains 256 values.  This
  514. tree is used to compress any data not compressed by the sliding
  515. dictionary algorithm.  When this tree is present, the Minimum
  516. Match Length for the sliding dictionary is 3.  If this tree is
  517. not present, the Minimum Match Length is 2.
  518.  
  519. The Length Shannon-Fano tree is used to compress the Length part
  520. of the (length,distance) pairs from the sliding dictionary
  521. output.  The Length tree contains 64 values, ranging from the
  522. Minimum Match Length, to 63 plus the Minimum Match Length.
  523.  
  524. The Distance Shannon-Fano tree is used to compress the Distance
  525. part of the (length,distance) pairs from the sliding dictionary
  526. output. The Distance tree contains 64 values, ranging from 0 to
  527. 63, representing the upper 6 bits of the distance value.  The
  528. distance values themselves will be between 0 and the sliding
  529. dictionary size, either 4K or 8K.
  530.  
  531. The Shannon-Fano trees themselves are stored in a compressed
  532. format. The first byte of the tree data represents the number of
  533. bytes of data representing the (compressed) Shannon-Fano tree
  534. minus 1.  The remaining bytes represent the Shannon-Fano tree
  535. data encoded as:
  536.  
  537.     High 4 bits: Number of values at this bit length + 1. (1 - 16)
  538.     Low  4 bits: Bit Length needed to represent value + 1. (1 - 16)
  539.  
  540. The Shannon-Fano codes can be constructed from the bit lengths
  541. using the following algorithm:
  542.  
  543. 1)  Sort the Bit Lengths in ascending order, while retaining the
  544.     order of the original lengths stored in the file.
  545.  
  546. 2)  Generate the Shannon-Fano trees:
  547.  
  548.     Code <- 0
  549.     CodeIncrement <- 0
  550.     LastBitLength <- 0
  551.     i <- number of Shannon-Fano codes - 1   (either 255 or 63)
  552.  
  553.     loop while i >= 0
  554.     Code = Code + CodeIncrement
  555.     if BitLength(i) <> LastBitLength then
  556.         LastBitLength=BitLength(i)
  557.         CodeIncrement = 1 shifted left (16 - LastBitLength)
  558.     ShannonCode(i) = Code
  559.     i <- i - 1
  560.     end loop
  561.  
  562.  
  563. 3)  Reverse the order of all the bits in the above ShannonCode()
  564.     vector, so that the most significant bit becomes the least
  565.     significant bit.  For example, the value 0x1234 (hex) would
  566.     become 0x2C48 (hex).
  567.  
  568. 4)  Restore the order of Shannon-Fano codes as originally stored
  569.     within the file.
  570.  
  571. Example:
  572.  
  573.     This example will show the encoding of a Shannon-Fano tree
  574.     of size 8.  Notice that the actual Shannon-Fano trees used
  575.     for Imploding are either 64 or 256 entries in size.
  576.  
  577. Example:   0x02, 0x42, 0x01, 0x13
  578.  
  579.     The first byte indicates 3 values in this table.  Decoding the
  580.     bytes:
  581.         0x42 = 5 codes of 3 bits long
  582.         0x01 = 1 code  of 2 bits long
  583.         0x13 = 2 codes of 4 bits long
  584.  
  585.     This would generate the original bit length array of:
  586.     (3, 3, 3, 3, 3, 2, 4, 4)
  587.  
  588.     There are 8 codes in this table for the values 0 thru 7.  Using the
  589.     algorithm to obtain the Shannon-Fano codes produces:
  590.  
  591.                                   Reversed     Order     Original
  592. Val  Sorted   Constructed Code      Value     Restored    Length
  593. ---  ------   -----------------   --------    --------    ------
  594. 0:     2      1100000000000000        11       101          3
  595. 1:     3      1010000000000000       101       001          3
  596. 2:     3      1000000000000000       001       110          3
  597. 3:     3      0110000000000000       110       010          3
  598. 4:     3      0100000000000000       010       100          3
  599. 5:     3      0010000000000000       100        11          2
  600. 6:     4      0001000000000000      1000      1000          4
  601. 7:     4      0000000000000000      0000      0000          4
  602.  
  603.  
  604. The values in the Val, Order Restored and Original Length columns
  605. now represent the Shannon-Fano encoding tree that can be used for
  606. decoding the Shannon-Fano encoded data.  How to parse the
  607. variable length Shannon-Fano values from the data stream is beyond the
  608. scope of this document.  (See the references listed at the end of
  609. this document for more information.)  However, traditional decoding
  610. schemes used for Huffman variable length decoding, such as the
  611. Greenlaw algorithm, can be succesfully applied.
  612.  
  613. The compressed data stream begins immediately after the
  614. compressed Shannon-Fano data.  The compressed data stream can be
  615. interpreted as follows:
  616.  
  617. loop until done
  618.     read 1 bit from input stream.
  619.  
  620.     if this bit is non-zero then       (encoded data is literal data)
  621.     if Literal Shannon-Fano tree is present
  622.         read and decode character using Literal Shannon-Fano tree.
  623.     otherwise
  624.         read 8 bits from input stream.
  625.     copy character to the output stream.
  626.     otherwise                   (encoded data is sliding dictionary match)
  627.     if 8K dictionary size
  628.         read 7 bits for offset Distance (lower 7 bits of offset).
  629.     otherwise
  630.         read 6 bits for offset Distance (lower 6 bits of offset).
  631.     
  632.     using the Distance Shannon-Fano tree, read and decode the
  633.       upper 6 bits of the Distance value.
  634.  
  635.     using the Length Shannon-Fano tree, read and decode
  636.       the Length value.
  637.  
  638.     Length <- Length + Minimum Match Length
  639.  
  640.     if Length = 63 + Minimum Match Length
  641.         read 8 bits from the input stream,
  642.         add this value to Length.
  643.  
  644.     move backwards Distance+1 bytes in the output stream, and
  645.     copy Length characters from this position to the output
  646.     stream.  (if this position is before the start of the output
  647.     stream, then assume that all the data before the start of
  648.     the output stream is filled with zeros).
  649. end loop
  650.  
  651. Tokenizing - Method 7
  652. ---------------------
  653.  
  654. Not currently supported by PKZIP or PKUNZIP.
  655.  
  656. ?????? - Method 8
  657. -----------------
  658.  
  659. The ?????? algorithm is similiar to the Implode algorithm using
  660. a sliding dictionary of up to 32K with secondary compression
  661. from dynamic Huffman/Shannon-Fano codes.
  662.  
  663. The compressed data is stored in blocks with a header describing
  664. the block and the huffman codes used in the data block.  The header
  665. format is as follows:
  666.  
  667.    Bit 1: Last Block bit     This bit is set to 1 if this is the last
  668.                              compressed block in the data.
  669.    Bits 2-3: Block type
  670.       00 (0) - Block is stored - All stored data is byte aligned.
  671.                Skip bits until next byte, then next word = block length,
  672.                followed by the ones compliment of the block length word.
  673.                Remaining data in block is the stored data.
  674.  
  675.       01 (1) - Use fixed huffman codes for literal and distance codes.
  676.                Lit Code    Bits             Dist Code   Bits
  677.                ---------   ----             ---------   ----
  678.                  0 - 143    8                 0 - 31      5
  679.                144 - 255    9
  680.                256 - 279    7
  681.                280 - 287    8
  682.  
  683.       10 (2) - Dynamic huffman codes.  (See expanding huffman codes)
  684.  
  685.       11 (3) - Reserved - Flag a "Error in compressed data" if seen.
  686.  
  687. Expanding Huffman Codes
  688. -----------------------
  689. If the data block is stored with dynamic huffman codes, the huffman
  690. codes are sent in the following compressed format:
  691.  
  692.    5 Bits: # of Literal codes sent + 255 (256 - 286)
  693.            All other codes are never sent.
  694.    5 Bits: # of Dist codes - 1           (1 - 32)
  695.    4 Bits: # of Bit Length codes - 3     (3 - 19)
  696.  
  697. The Huffman codes are sent as bit lengths and the codes are built as
  698. described in the implode algorithm.  The Bit lengths themselves are
  699. compressed with huffman codes.  There are 19 bit length codes:
  700.  
  701.    0 - 15: Represent bit lengths of 0 - 15
  702.        16: Copy the Previous bit length 3 - 6 times.
  703.            The next 2 bits indicate repeat length (0 = 3, ... ,3 = 6)
  704.        17: Repeat a bit length of 0 for 3 - 10 times. (3 bits of length)
  705.        18: Repeat a bit length of 0 for 11 - 138 times (7 bits of length)
  706.  
  707. The lengths of the bit length codes are sent packed 3 bits per value
  708. (0 - 7) in the following order:
  709.  
  710.    16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
  711.  
  712. The huffman codes should be built as described in the Implode alogrithm
  713. except codes are assigned starting at the shortest bit length, i.e. the
  714. shortest code should be all 0's rather than all 1's.  These codes are
  715. then used to decode the bit lengths for the literal and distance tables.
  716.  
  717. The bit lengths for the literal tables are sent first with the number
  718. of entries sent described by the 5 bits sent earlier.  There are up
  719. to 288 literal characters, the first 256 represent the respective 8
  720. bit character, code 257 represents the End-Of-Block code, the remaining
  721. 29 codes represent copy lengths of 3 thru 258.  There are up to 30
  722. distance codes representing distances from 1 thru 32k as described
  723. below.
  724.  
  725.                              Length Codes
  726.                              ------------
  727.       Extra             Extra              Extra              Extra
  728.  Code Bits Length  Code Bits Lengths  Code Bits Lengths  Code Bits Length(s)
  729.  ---- ---- ------  ---- ---- -------  ---- ---- -------  ---- ---- ---------
  730.   257   0     3     265   1   11,12    273   3   35-42    281   5  131-162
  731.   258   0     4     266   1   13,14    274   3   43-50    282   5  163-194
  732.   259   0     5     267   1   15,16    275   3   51-58    283   5  195-226
  733.   260   0     6     268   1   17,18    276   3   59-66    284   5  227-257
  734.   261   0     7     269   2   19-22    277   4   67-82    285   0    258
  735.   262   0     8     270   2   23-26    278   4   83-98
  736.   263   0     9     271   2   27-30    279   4   99-114
  737.   264   0    10     272   2   31-34    280   4  115-130
  738.  
  739.                             Distance Codes
  740.                             --------------
  741.       Extra           Extra             Extra               Extra
  742.  Code Bits Dist  Code Bits  Dist   Code Bits Distance  Code Bits Distance
  743.  ---- ---- ----  ---- ---- ------  ---- ---- --------  ---- ---- --------
  744.    0   0    1      8   3   17-24    16    7  257-384    24   11  4097-6144
  745.    1   0    2      9   3   25-32    17    7  385-512    25   11  6145-8192
  746.    2   0    3     10   4   33-48    18    8  513-768    26   12  8193-12288
  747.    3   0    4     11   4   49-64    19    8  769-1024   27   12 12289-16384
  748.    4   1   5,6    12   5   65-96    20    9 1025-1536   28   13 16385-24576
  749.    5   1   7,8    13   5   97-128   21    9 1537-2048   29   13 24577-32768
  750.    6   2   9-12   14   6  129-192   22   10 2049-3072
  751.    7   2  13-16   15   6  193-256   23   10 3073-4096
  752.  
  753. The compressed data stream begins immediately after the
  754. compressed header data.  The compressed data stream can be
  755. interpreted as follows:
  756.  
  757. do
  758.    read header from input stream.
  759.  
  760.    if stored block
  761.       skip bits until byte aligned
  762.       read count and 1's compliment of count
  763.       copy count bytes data block
  764.    otherwise
  765.       loop until end of block code sent
  766.          decode literal character from input stream
  767.          if literal < 256
  768.             copy character to the output stream
  769.          otherwise
  770.             if literal = end of block
  771.                break from loop
  772.             otherwise
  773.                decode distance from input stream
  774.  
  775.                move backwards distance bytes in the output stream, and
  776.                copy length characters from this position to the output
  777.                stream.
  778.       end loop
  779. while not last block
  780.  
  781.  
  782. Decryption
  783. ----------
  784.  
  785. The encryption used in PKZIP was generously supplied by Roger
  786. Schlafly.  PKWARE is grateful to Mr. Schlafly for his expert
  787. help and advice in the field of data encryption.
  788.  
  789. PKZIP encrypts the compressed data stream.  Encrypted files must
  790. be decrypted before they can be extracted.
  791.  
  792. Each encrypted file has an extra 12 bytes stored at the start of
  793. the data area defining the encryption header for that file.  The
  794. encryption header is originally set to random values, and then
  795. itself encrypted, using 3, 32-bit keys.  The key values are
  796. initialized using the supplied encryption password.  After each byte
  797. is encrypted, the keys are then updated using psuedo-random number
  798. generation techniques in combination with the same CRC-32 algorithm
  799. used in PKZIP and described elsewhere in this document.
  800.  
  801. The following is the basic steps required to decrypt a file:
  802.  
  803. 1) Initialize the three 32-bit keys with the password.
  804. 2) Read and decrypt the 12-byte encryption header, further
  805.    initializing the encryption keys.
  806. 3) Read and decrypt the compressed data stream using the
  807.    encryption keys.
  808.  
  809.  
  810. Step 1 - Initializing the encryption keys
  811. -----------------------------------------
  812.  
  813. Key(0) <- 305419896
  814. Key(1) <- 591751049
  815. Key(2) <- 878082192
  816.  
  817. loop for i <- 0 to length(password)-1
  818.     update_keys(password(i))
  819. end loop
  820.  
  821.  
  822. Where update_keys() is defined as:
  823.  
  824.  
  825. update_keys(char):
  826.   Key(0) <- crc32(key(0),char)
  827.   Key(1) <- Key(1) + (Key(0) & 000000ffH)
  828.   Key(1) <- Key(1) * 134775813 + 1
  829.   Key(2) <- crc32(key(2),key(1) >> 24)
  830. end update_keys
  831.  
  832.  
  833. Where crc32(old_crc,char) is a routine that given a CRC value and a
  834. character, returns an updated CRC value after applying the CRC-32
  835. algorithm described elsewhere in this document.
  836.  
  837.  
  838. Step 2 - Decrypting the encryption header
  839. -----------------------------------------
  840.  
  841. The purpose of this step is to further initialize the encryption
  842. keys, based on random data, to render a plaintext attack on the
  843. data ineffective.
  844.  
  845.  
  846. Read the 12-byte encryption header into Buffer, in locations
  847. Buffer(0) thru Buffer(11).
  848.  
  849. loop for i <- 0 to 11
  850.     C <- buffer(i) ^ decrypt_byte()
  851.     update_keys(C)
  852.     buffer(i) <- C
  853. end loop
  854.  
  855.  
  856. Where decrypt_byte() is defined as:
  857.  
  858.  
  859. unsigned char decrypt_byte()
  860.     local unsigned short temp
  861.     temp <- Key(2) | 2
  862.     decrypt_byte <- (temp * (temp ^ 1)) >> 8
  863. end decrypt_byte
  864.  
  865.  
  866. After the header is decrypted, the last two bytes in Buffer
  867. should be the high-order word of the CRC for the file being
  868. decrypted, stored in Intel low-byte/high-byte order.  This can
  869. be used to test if the password supplied is correct or not.
  870.  
  871.  
  872. Step 3 - Decrypting the compressed data stream
  873. ----------------------------------------------
  874.  
  875. The compressed data stream can be decrypted as follows:
  876.  
  877.  
  878. loop until done
  879.     read a charcter into C
  880.     Temp <- C ^ decrypt_byte()
  881.     update_keys(temp)
  882.     output Temp
  883. end loop
  884.  
  885.  
  886. In addition to the above mentioned contributors to PKZIP and PKUNZIP,
  887. I would like to extend special thanks to Robert Mahoney for suggesting
  888. the extension .ZIP for this software.
  889.  
  890.  
  891. References:
  892.  
  893.     Storer, James A. "Data Compression, Methods and Theory",
  894.        Computer Science Press, 1988
  895.  
  896.     Held, Gilbert  "Data Compression, Techniques and Applications,
  897.             Hardware and Software Considerations"
  898.        John Wiley & Sons, 1987
  899.